package com.six.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.six.common.BizException;
import com.six.domain.dto.PayConfigDto;
import com.six.domain.dto.PrinterDto;
import com.six.domain.entity.PayConfig;
import com.six.domain.entity.Printer;
import com.six.domain.entity.Store;
import com.six.domain.entity.SysDict;
import com.six.domain.vo.PrinterVo;
import com.six.mapper.PayConfigMapper;
import com.six.mapper.PrinterMapper;
import com.six.mapper.StoreMapper;
import com.six.mapper.SysDictMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class SysAdminService {
    private final StoreMapper storeMapper;
    private final PrinterMapper printerMapper;
    private final PayConfigMapper payConfigMapper;
    private final SysDictMapper dictMapper;

    /* ---------- 门店 ---------- */
    public Page<Store> listStores(String keyword, Page<Store> page) {
        long total = storeMapper.countByKeyword(keyword);
        List<Store> records = storeMapper.selectPage(keyword,
                (page.getCurrent() - 1) * page.getSize(),
                (int) page.getSize());
        page.setTotal(total);
        page.setRecords(records);
        return page;
    }

    public void deleteStore(Long id) {
        storeMapper.deleteById(id);
    }

    /* ---------- 打印机 ---------- */
    public void savePrinter(PrinterDto dto) {
        Printer p = new Printer();
        BeanUtils.copyProperties(dto, p);
        printerMapper.insert(p);
    }

    public List<PrinterVo> listPrinters(Long storeId) {
        List<Printer> list = printerMapper.selectByStore(storeId);
        if (storeId == null) {          // ADMIN 查全部
            list = printerMapper.selectList(null);
        } else {                        // 按门店查
            list = printerMapper.selectByStore(storeId);
        }
        List<PrinterVo> voList = list.stream()
                .map(p -> {
                    PrinterVo v = new PrinterVo();
                    BeanUtils.copyProperties(p, v);
                    return v;
                })
                .collect(Collectors.toList());

        log.info("🎯 转 VO 后条数: {}", voList.size());
        return voList;
    }
    public void deletePrinter(Long id) {
        if (printerMapper.deleteById(id) == 0) {
            throw new BizException("打印机不存在");
        }
    }
    public void updatePrinter(PrinterDto dto) {
        Printer p = new Printer();
        BeanUtils.copyProperties(dto, p); // 现在会把 enabled 拷进去
        p.setId(dto.getId());
        if (printerMapper.updateById(p) == 0) {
            throw new BizException("打印机不存在");
        }
    }


    /* ---------- 支付配置 ---------- */
    public void savePayConfig(PayConfigDto dto) {
        PayConfig c = new PayConfig();
        BeanUtils.copyProperties(dto, c);
        payConfigMapper.insert(c);
    }

    public List<PayConfig> listPayConfigs(Long storeId) {
        return payConfigMapper.selectByStore(storeId);
    }

    /* ---------- 字典 ---------- */
    public List<SysDict> listDict(String type) {
        return dictMapper.selectByType(type);
    }

    public void updateDict(String type, List<SysDict> items) {
        dictMapper.deleteByType(type);
        items.forEach(d -> {
            d.setType(type);
            dictMapper.insert(d);
        });
    }
}